require.config({
	baseUrl: "./js",
	paths: {
		"jQuery": ["lib/jquery-2.1.0"],
		"song": ["lib/song_sheet"],
		"create": ["lib/create"]
	}
})
require(['jQuery', 'song', 'create'], function(jq) {
	var queue = new createjs.LoadQueue();
	queue.on('progress', function(e) {
		var p = e.progress * 100 + '%';
		$('.yujiazai>.Loading>span')[0].innerHTML =
			`
		${p}
		`
	})
	queue.on('complete', function() {
		$('.box').css({
			'display': 'block'
		})
		$('.yujiazai').css({
			'display': 'none'
		})
	})
	queue.loadManifest([{
			id: 1,
			src: "img/audienceLeft.png"
		},
		{
			id: 1,
			src: "img/audienceRight.png"
		},
		{
			id: 1,
			src: "img/man_left.png"
		},
		{
			id: 1,
			src: "img/man_right.png"
		},
		{
			id: 1,
			src: "img/buttonleft.png"
		},
		{
			id: 1,
			src: "img/buttonright.png"
		},
		{
			id: 1,
			src: "img/chengq.png"
		},
		{
			id: 1,
			src: "img/earth.png"
		},
		{
			id: 1,
			src: "img/feiZhao.png"
		},
		{
			id: 1,
			src: "img/huangjing.png"
		},
		{
			id: 1,
			src: "img/index_625secguess.png"
		},
		{
			id: 1,
			src: "img/index_start.png"
		},
		{
			id: 1,
			src: "img/manwoman_bg.png"
		},
		{
			id: 1,
			src: "img/bg.jpg"
		},
		{
			id: 1,
			src: "img/mouth.png"
		},
		{
			id: 1,
			src: "img/movieHitSong.png"
		},
        {
			id: 1,
			src: "img/movieHitSongpeople.png"
		},
		{
			id: 1,
			src: "img/sangBan1.png"
		},
		{
			id: 1,
			src: "img/sangBan2.png"
		},
		{
			id: 1,
			src: "img/sixQuery.png"
		},
		{
			id: 1,
			src: "img/smallMouth.png"
		},
		{
			id: 1,
			src: "img/songer.png"
		},
		{
			id: 1,
			src: "img/stage.png"
		},
		{
			id: 1,
			src: "img/star.png"
		},
		{
			id: 1,
			src: "img/text.png"
		},
		{
			id: 1,
			src: "img/touxinag.png"
		},
		{
			id: 1,
			src: "img/user_bg.png"
		},
		{
			id: 1,
			src: "img/userint.png"
		},
		{
			id: 1,
			src: "img/watchpeos.png"
		},
		{
			id: 1,
			src: "img/WCwalls.png"
		},
		{
			id: 1,
			src: "img/what.png"
		},
		{
			id: 1,
			src: "img/whatSong.png"
		},
		{
			id: 1,
			src: "img/wogeiyou.png"
		},
		{
			id: 1,
			src: "img/wow.png"
		},
		{
			id: 1,
			src: "img/wow2.png"
		},
		{
			id: 1,
			src: "img/wuman.png"
		},
		{
			id: 1,
			src: "img/xuanA.png"
		},
		{
			id: 1,
			src: "img/xuanB.png"
		},
		{
			id: 1,
			src: "img/xuanC.png"
		},
		{
			id: 1,
			src: "img/car.png"
		},
		{
			id: 1,
			src: "img/threeYao.png"
		},
	    {
			id: 1,
			src: "img/hua.png"
		},
	])
	
	$('audio')[0].loop = true;
	//	console.log(mus_arr[0]);
	//开关
	var flag = true,
		num,
		xuan_flag = true,
		nums, arr = [],
		num2 = 0,
		cont = 0,
		//用于计时
		time = 60,
		//用于记录答对的个数
		frequency = 0,
		width,
		height,
		t;
	function suijidaan() {
		for(let i = 0; i < mus_arr.length; i++) {
			nums = Math.random() * (mus_arr.length);
			nums = Math.floor(nums);
			if(i == 0) {
				arr[i] = nums
			} else {
				for(let j = 0; j < arr.length; j++) {
					if(nums == arr[j]) {
						cont++;
					}
				}
				if(cont <= 0) {
					arr[i] = nums
				} else {
					i--;
					cont = 0;
				}
			}

		}
	}
	suijidaan();
	height = window.screen.availHeight;
	width = window.screen.availWidth;
	$('.GO').css({
		'width': width,
		'height': height
	})
	$('.box').css({
		'width': width,
		'height': height
	})
	$('.start>img').on('click', () => {
		//		$('.go').css({
		//   		'display':'block'
		//   	})
		$('.GO').css({
			'opacity': 1
		})
		$('.GO>.clock')[0].innerHTML =
			`
            ${time}
            `
		t = setInterval(function() {
			if(time <= 60 && time > 50) {
				$('.GO>.GO_scene>.scene1').siblings().css({
					'display': 'none'
				})

				$('.GO>.GO_scene>.scene1').css({
					'display': 'block'
				})
			}

			if(time <= 50 && time > 40) {
				$('.GO>.GO_scene>.scene2').siblings().css({
					'display': 'none'
				})

				$('.GO>.GO_scene>.scene2').css({
					'display': 'block'
				})

				$('.GO>.GO_scene>.scene2').animate({
					'opacity': 1
				}, 650)
			}
			if(time <= 40 && time >30) {
				$('.GO>.GO_scene>.scene3').siblings().css({
					'display': 'none'
				})

				$('.GO>.GO_scene>.scene3').css({
					'display': 'block'
				})

				$('.GO>.GO_scene>.scene3').animate({
					'opacity': 1
				}, 650)
			}
			if(time <= 30 && time >20) {
				$('.GO>.GO_scene>.scene4').siblings().css({
					'display': 'none'
				})

				$('.GO>.GO_scene>.scene4').css({
					'display': 'block'
				})

				$('.GO>.GO_scene>.scene4').animate({
					'opacity': 1
				}, 650)
			}
			if(time <= 20 && time >=0) {
				$('.GO>.GO_scene>.scene5').siblings().css({
					'display': 'none'
				})

				$('.GO>.GO_scene>.scene5').css({
					'display': 'block'
				})

				$('.GO>.GO_scene>.scene5').animate({
					'opacity': 1
				}, 650)
			}
			if(time <= 0) {
				
				clearInterval(t);
				$('audio')[0].pause();
				//				return;
				//时间结束 游戏结束
				//console.log('游戏结束了')
				$('.finish>.info>span')[0].innerHTML =
					`
                ${frequency}
                `
				if(frequency > 0) {
					$('.finish>.info>span')[0].innerHTML =
						`
                ${frequency}
                `
				}

				$('.GO').animate({
					'opacity': 0
				}, 750)
				var t2 = setTimeout(function() {
					$('.GO').css({
						'display': 'none'
					})
					$('.finish').css({
						'display': 'block'
					})
					clearTimeout(t2)
				}, 600)

			} else {
				time--;
				//			console.log(time);
				$('.GO>.clock')[0].innerHTML =
					`
            ${time}
            `
			}

		}, 1000)
		if(flag == true) {
			flag = false;
			//		console.log(flag)	
			//	console.log('点击')
			var p = new Promise(opacity).then(stmp)
		}
	})

	function opacity(resolve) {
		$('.box>div').animate({
			'opacity': 0
		}, 750)
		setTimeout(function() {
			// 		console.log(1)
			resolve(flag)
		}, 750)
	}

	function stmp(flag) {
		console.log('已清除');
		$('.GO').css({
			"display": "block"
		});
		$('.box').css({
			'display': 'none'
		})
		$('.xuan>div')[0].innerHTML =
			`
        ${mus_arr[arr[0]].a};
        `
		$('.xuan>div')[1].innerHTML =
			`
        ${mus_arr[arr[0]].b}
        `
		$('.xuan>div')[2].innerHTML =
			`
        ${mus_arr[arr[0]].c}
        `
		$('audio')[0].src = mus_arr[arr[0]].music_url
		//	console.log(arr.length)
		$('audio')[0].play();
	}
	$('.xuan').on('click', function(e) {
		//		console.log(arr.length) ;
		if(xuan_flag == true) {
			xuan_flag = false;

			num2++;
			if(num2 <= arr.length - 1) {

				if($(e.target).is('div') == true) {
					$('audio')[0].pause();

					for(var i = 0; i < $('.xuan').children().size(); i++) {

						if($(e.target).text() == $('.xuan>div').eq(i).text()) {
							num = i;
							if(mus_arr[arr[num2 - 1]].answer == num) {
								console.log('答案正确')
								frequency++;
							}
						}
					}
					if(nums < mus_arr.length) {
						var p2 = new Promise(temp1).then(temp2)
					} else {
						return;
					}

					function temp1(resolve) {
						$('.xuan').animate({
							'opacity': 0
						}, 750)
						setTimeout(function() {
							resolve(xuan_flag)
						}, 750)
					}

					function temp2(flag) {
						$('.xuan>div')[0].innerHTML =
							`
        ${mus_arr[arr[num2]].a}
        `
						$('.xuan>div')[1].innerHTML =
							`
        ${mus_arr[arr[num2]].b}
        `
						$('.xuan>div')[2].innerHTML =
							`
        ${mus_arr[arr[num2]].c}
        `
						$('audio')[0].src = mus_arr[arr[num2]].music_url
						$('audio')[0].play();
						$('.xuan').animate({
							'opacity': 1
						}, 750)
						setTimeout(function() {
							xuan_flag = true;

						}, 500)
					}

				}
			} else {
				//				console.log('游戏结束了');
				time = 0;

				$('.finish>.info>span')[0].innerHTML =
					`
                ${frequency}
                `

				$('.GO').animate({
					'opacity': 0
				}, 750)
				setTimeout(function() {
					$('.GO').css({
						'display': 'none'
					})
					$('.finish').css({
						'display': 'block'
					})
				}, 600)
			}
		}

	})
	$('.buttonleft').on('click', function() {
		flag = true
		xuan_flag = true
		time = 60;
		frequency = 0;
		arr = undefined;
		arr = [];
		num2 = 0;
		cont = 0;
		clearInterval(t);
		suijidaan();
		$('.finish').css({
			'display': 'none'
		})

		$('.box').css({
			'display': 'block'
		})

		$('.box>div').css({
			'opacity': 1
		})

	})
})

document.body.style.height = window.innerHeight + "px";

/*动态改变根元素字体大小*/
function recalc() {
	//获取客户端宽度
	var clientWidth = document.documentElement.clientWidth;
	if(!clientWidth) return;
	// 字体大小   = 1个rem单位表示多少个像素（设备的宽度   /设计宽度）
	document.documentElement.style.fontSize = 40 * (clientWidth / 750) + 'px';
}

function initRecalc() {
	recalc();
	// if(浏览器支持横竖屏切换的事件){横竖屏事件}else{ resize事件 }
	var resizeEvt = 'osrientationchange' in window ? 'orientationchange' : 'resize';
	if(!document.addEventListener) return;
	window.addEventListener(resizeEvt, recalc, false);
	document.addEventListener('DOMContentLoaded', recalc, false);
}
initRecalc();
//			
//$('.start>img').on('click',function(){
//	console.log('点击')
//})